<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/3/17
 * Time: 15:22
 */

namespace App\Rep;


use App\Exceptions\ApiException;
use App\Models\Order;
use App\Models\OrderChild;
use App\Models\ProductCenter;
use Illuminate\Support\Facades\DB;
use PHPUnit\Exception;

class OrderChildRep
{

    /**获取生产订单列表**/
    public function getOrderChildListRep($params){
        $list = Order::Schedule($params)->where('is_replenishment',2)
            ->with(['color'=>function($q){
                return $q->select('id','color');
            },'product'=>function($q){
                return $q->select('id','product_name')->with('job_type');
            },'customer'=>function($q){
                return $q->select('id','customer_name','customer_address','contact_phone');
            },'modelNumber'=>function($q){
                return $q->select('id','model_number','file_id')->with(['file']);
            },'orderChild'=>function($q){
                return $q->where('is_del',2)->with(['job_type','employee'])->orderBy('job_type_id');
            },'specification'=>function($q){
                return $q->select('id','specification');
            },'dealer'=>function($q){
                return $q->select('id','company_name');
            },'order_remark'])
            ->orderBy('id','desc')
            ->paginate($params['per_page']??10);
        return $list;
    }

    /**指派订单**/
    public function assignOrderChildRep($params){
        $child_order_id = $params['child_order_id'];//子订单ID
        $allocate_num = $params['allocate_num'];
        $orderChild = OrderChild::where('id',$child_order_id)->first();
        try {
            DB::beginTransaction();
            if($orderChild){
                if($orderChild->num < $allocate_num){
                    throw new ApiException(20001,'可分配数不足');
                }elseif ($orderChild->num == $allocate_num){
                    $orderChild->num = 0;
                    $orderChild->is_del = 1;
                }else{
                    $orderChild->num = $orderChild->num - $allocate_num;
                }
                $model = new OrderChild();
                $model->order_id = $orderChild->order_id;
                $model->job_type_id = $orderChild->job_type_id;
                $model->price = $orderChild->price;
                $model->num = $allocate_num;
                $model->operator_user_id = $params['user_id'];//操作人
                $model->employee_user_id = $params['employee_user_id'];//指派员工ID
                $model->appoint_time = date("Y-m-d H:i:s",time());//订单领取/指派时间
                $model->is_appoint = 1;//指派
                $model->save();

                $orderChild->operator_user_id = $params['user_id'];//操作人
                $orderChild->save();
            }else{
                throw new ApiException(20001,'可分配数不足');
            }
            $orderRep = new OrderRep();
            $orderRep->setOrderProductionStatus($orderChild->order_id,1);//生产中
            DB::commit();
            return ['code'=>0,'msg'=>'success','data'=>[]];
        }catch (Exception $exception){
            DB::rollBack();
            return ['code'=>2001,'msg'=>$exception->getMessage(),'data'=>[]];
        }

    }

    /**外包订单**/
    public function epibolyOrderChildRep($params){
        //p($params);
        $child_order_id = $params['child_order_id'];//子订单ID
        $orderChild = OrderChild::where('id',$child_order_id)->first();
        //$orderChild->operator_user_id = $params['user_id'];//操作人
        $orderChild->epiboly_time = date("Y-m-d H:i:s",time());//订单外包时间
        $orderChild->is_epiboly = 1;
        //$orderChild->is_appoint = 2;
        $orderChild->status = 2;
        $orderChild->save();
        $orderRep = new OrderRep();
        $orderRep->setOrderProductionStatus($orderChild->order_id,1);//生产中
        return $orderChild;
    }

    /**
     * 根据订单id获取子订单列表
     * @param $params
     */
    public function getOrderChildListByOrderIdRep($params){
        $order = Order::where('id',$params['order_id'])
            ->with(['color','product','customer','modelNumber','orderChild'=>function($q){
                $q->where('is_del',2)->orderBy('job_type_id','asc')->with(['job_type','employee','image1'=>function($q){
                    return $q->select('id','file_name','file_url');
                },'image2'=>function($q){
                    return $q->select('id','file_name','file_url');
                },'image3'=>function($q){
                    return $q->select('id','file_name','file_url');
                },'image4'=>function($q){
                    return $q->select('id','file_name','file_url');
                }]);
            }])
            ->first()->toArray();
        return $order['order_child'];
    }

    /**
     * 设置子订单状态为【完成】
     * @param $params
     */
    public function setOrderChildStatusRep($params){
        DB::beginTransaction();
        try{
            $child_order_id = $params['child_order_id'];//子订单ID
            $orderChild = OrderChild::where('id',$child_order_id)->first();
            $orderChild->operator_user_id = $params['user_id'];//操作人
            if($orderChild->is_epiboly == 1){
                //已外包
                $orderChild->status = 1;
                $orderChild->submit_time = date("Y-m-d H:i:s",time());//提交时间
            }else{
                if(empty($params['employee_user_id'])){
                    return ['code'=>1019,'msg'=>'尚未指派员工','data'=>[]];
                }
                $orderChild->employee_user_id = $params['employee_user_id'];//指派员工ID
                $orderChild->appoint_time = date("Y-m-d H:i:s",time());//订单领取/指派时间
                $orderChild->is_appoint = 1;
                //$orderChild->is_epiboly = 2;
                $orderChild->status = 1;
                $orderChild->draw_time = date("Y-m-d H:i:s",time());//领取时间
                $orderChild->submit_time = date("Y-m-d H:i:s",time());//提交时间
            }
            $orderChild->save();
            $orderRep = new OrderRep();
            $orderRep->setOrderProductionStatus($orderChild->order_id,1);//生产中
            $orderRep->selectOrderChildStatus($orderChild->order_id);//加入半成品库
            DB::commit();
            return ['code'=>0,'msg'=>'success','data'=>$orderChild];
        }catch(\Exception $exception){
            DB::rollBack();
            return ['code'=>2001,'msg'=>$exception->getMessage(),'data'=>[]];
        }

    }

    /**
     * 自动添加子订单
     * @param $product_id
     * @param $order_id
     */
    public function autoCreateOrderChild($product_id,$order_id,$model_number_id,$specification_id,$num){
        $productRep = new ProductRep();
        $job_type_list = $productRep->getProductJobTypeListByProductId($product_id);
        $jobTypeRep = new JobTypeRep();
        foreach($job_type_list as $k=>$v){
            $price = ProductCenter::where('product_id',$product_id)
                ->where('model_number_id',$model_number_id)
                ->where('specification_id',$specification_id)
                ->where('job_type_id',$v['id'])
                ->value('price');
            $where = ['order_id'=>$order_id, 'job_type_id'=>$v['id']];
            //$sort = $jobTypeRep->getProductJobTypeSort($product_id,$v['id']);
            OrderChild::updateOrCreate(
                $where,
                ['order_id'=>$order_id, 'job_type_id'=>$v['id'],'price'=>$price??0,'num'=>$num]);
        }
    }


}
